To produce a set of diagnostic plots that will be included in the report. Please note that these plots are just meant to provide an example of what could be created and how. They are not an exhaustive list of every possible plot and were chosen with the project aims in mind.
While this should give users examples of plots generated with the most up-to-date packages and methods, we’re always happy to have feedback. If you know of more efficient methods or want to suggest alternative ways of plotting the figures please open an issue with the details.
Define modelName and path to the model directory
(MODEL_DIR).
If saving figures out to pdf, define where those pdfs should be saved
to. Here the figures are saved to
deliv > figure > model_run_number
Read in posterior samples from the .ext files. If
available, also read in the individual posteriors and objective function
values for calculating shrinkage and LOO-CV. If individual posteriors
are not available, calculated medians of shrinkages reported by NONMEM
instead.
A summary of high-level model details using only output from the first chain.
Read in the model details using read_model. Details
stored in the mod object can be used to identify the
location of the source data (used in $DATA) - to see how this is done
look at the bbr::get_data_path() and
bbr::build_path_from_model() helper functions.
── Status ─────────────────────────────────
• Finished Running
── Absolute Model Path ───────────────────────────
• /data/iu-nonmem-bayes-2023/model/nonmem/200/200_1
── YAML & Model Files ───────────────────────────
• /data/iu-nonmem-bayes-2023/model/nonmem/200/200_1.yaml
• /data/iu-nonmem-bayes-2023/model/nonmem/200/200_1.ctl
── Description ───────────────────────────────
• Chain 1
Dataset: ../../../../data/derived/study-001-002-003.csv
Records: 3428 Observations: 2572 Subjects: 208
Objective Function Value (final est. method): 23796.279
Estimation Method(s):
– Chain Method Processing
– NUTS Bayesian Analysis
No Heuristic Problems Detected
param_estimates() is not currently implemented for Bayesian methods.
Compute summaries of posterior distributions (now using all
params$n_chains chains), as well as some diagnostics.
Bulk effective sample size (ESS) is a measure of sampling efficiency for the location of the distribution, while Tail ESS is a measure of sampling efficiency for the tails (5% and 95% quantiles) of the distribution. Higher values indicate greater sampling efficiency. A very rough rule of thumb is to aim for at least 400 for each parameter.
R-hat is a convergence diagnostic that compares the between- and within-chain variances of model parameters. Values close to 1 indicate that the chains have converged to similar distributions. Aim for less than about 1.05 for all parameters.
| parameter | mean | median | 95% CI | Bulk_ESS | Tail_ESS | Rhat | shrinkage |
|---|---|---|---|---|---|---|---|
| THETA1 | 2.00 | 2.00 | (1.91,2.09) | 189 | 553 | 1.02 | NA |
| THETA2 | 5.26 | 5.26 | (5.20,5.32) | 389 | 818 | 1.01 | NA |
| THETA3 | 0.459 | 0.455 | (0.236,0.690) | 701 | 1699 | 1.00 | NA |
| THETA4 | 0.239 | 0.240 | (0.137,0.333) | 2652 | 2857 | 1.00 | NA |
| THETA5 | -0.594 | -0.598 | (-0.741,-0.432) | 2564 | 2100 | 1.00 | NA |
| THETA6 | 4.26 | 4.24 | (3.77,4.86) | 1897 | 1806 | 1.00 | NA |
| THETA7 | 0.753 | 0.751 | (0.558,0.958) | 329 | 798 | 1.02 | NA |
| THETA8 | 1.08 | 1.08 | (0.835,1.32) | 703 | 903 | 1.00 | NA |
| THETA9 | -0.0396 | -0.0413 | (-0.156,0.0818) | 431 | 707 | 1.01 | NA |
| THETA10 | 0.411 | 0.411 | (0.0873,0.723) | 423 | 659 | 1.01 | NA |
| THETA11 | -0.227 | -0.227 | (-0.304,-0.152) | 319 | 702 | 1.01 | NA |
| THETA12 | 0.0941 | 0.0960 | (0.0170,0.167) | 478 | 927 | 1.00 | NA |
| SIGMA(1,1) | 0.0444 | 0.0443 | (0.0416,0.0474) | 2789 | 3054 | 1.00 | NA |
| SIGMA(2,2) | 1.34 | 1.31 | (0.868,2.01) | 3418 | 2808 | 1.00 | NA |
| OMEGA(1,1) | 0.0927 | 0.0921 | (0.0754,0.114) | 415 | 809 | 1.01 | 4.96 |
| OMEGA(2,1) | 0.0769 | 0.0759 | (0.0579,0.102) | 489 | 866 | 1.01 | NA |
| OMEGA(2,2) | 0.131 | 0.130 | (0.102,0.166) | 663 | 1019 | 1.00 | 9.94 |
| OMEGA(3,3) | 1.01 | 0.993 | (0.733,1.40) | 923 | 1620 | 1.00 | 19.8 |
PSIS-LOO CV is leave-one-out (LOO) cross-validation for Bayesian models using Pareto smoothed importance sampling (PSIS). This is used for model comparison, similar to the -2LL objective function for non-Bayesian models.
##
## Computed from 4000 by 208 log-likelihood matrix
##
## Estimate SE
## elpd_loo 23458.9 923.5
## p_loo 2525.9 43.9
## looic -46917.8 1847.1
## ------
## Monte Carlo SE of elpd_loo is 0.4.
##
## Pareto k diagnostic values:
## Count Pct. Min. n_eff
## (-Inf, 0.5] (good) 205 98.6% 238
## (0.5, 0.7] (ok) 3 1.4% 234
## (0.7, 1] (bad) 0 0.0% <NA>
## (1, Inf) (very bad) 0 0.0% <NA>
##
## All Pareto k estimates are ok (k < 0.7).
## See help('pareto-k-diagnostic') for details.
The aim is to use the information in the spec file to label the figures automatically.
Model output (EPRED, IPRED, NPDE, EWRES, ETAs) is read in from either
the output of simulations (see run-sims.R) or from NONMEM
output only. When only NONMEM output is used, medians of these values
across all chains are calculated.
After reading in the NONMEM dataset and the output dataset they’re
joined by a NUM column. This assumes that a row
number column (called NUM) was included during data
assembly. The idea here is that in NONMEM, you table just
NUM and none of the other input data items. They all will
get joined back to the NONMEM output … even character columns.
The data used in the diagnostic plots has been filtered
to only include the observations (i.e. EVID==0). Note that
further modifications maybe needed, for example, if BLQ data was
included in the model or if the DV was log-transformed. The
dataset also converts the categorical covariates of interest to factors
using the yspec_add_factors function and details described
in the spec file.
The id subset gets the first record per ID. This would
usually be the baseline value but consider filtering on a baseline flag
if available. Also, if the model includes inter-occasion variability
(IOV), the occasion variable should be included within the
distinct function.
These plots assess whether all of the chains have converged to a single, stationary distribution.
## [[1]]
##
## [[2]]
##
## [[3]]
##
## [[4]]
##
## [[5]]
##
## [[6]]
##
## [[7]]
##
## [[8]]
##
## [[9]]
##
## [[10]]
##
## [[11]]
##
## [[12]]
##
## [[13]]
##
## [[14]]
##
## [[15]]
##
## [[16]]
##
## [[17]]
##
## [[18]]
Look for any strong correlation between parameters, indicating possible over-parameterisation.
Plots Bulk and Tail ESS versus iteration to check that ESS increases linearly.
## [[1]]
##
## [[2]]
##
## [[3]]
##
## [[4]]
##
## [[5]]
##
## [[6]]
##
## [[7]]
##
## [[8]]
##
## [[9]]
##
## [[10]]
##
## [[11]]
##
## [[12]]
##
## [[13]]
##
## [[14]]
##
## [[15]]
##
## [[16]]
##
## [[17]]
##
## [[18]]
Plots Bulk and Tail ESS versus quantile to better diagnose areas of the distributions that the iterative algorithm fails to explore efficiently.
## [[1]]
##
## [[2]]
##
## [[3]]
##
## [[4]]
##
## [[5]]
##
## [[6]]
##
## [[7]]
##
## [[8]]
##
## [[9]]
##
## [[10]]
##
## [[11]]
##
## [[12]]
##
## [[13]]
##
## [[14]]
##
## [[15]]
##
## [[16]]
##
## [[17]]
##
## [[18]]
The following plots assume that the preferred x-axis labels are defined here.
Create plots of DV vs population and individual predictions for the full dataset and stratified by renal function and hepatic function.
Population predictions are medians of 2000 simulated values incorporating between- and within-subject variability, as well as uncertainty in population parameter estimates via sampling from the posterior distribution. Individual predictions are medians of 2000 simulated values incorporating conditional estimates of individual parameters and include within-subject variability, as well as uncertainty in population parameter estimates via sampling from the posterior distribution.
Normalized prediction distribution errors (NPDE) are Monte-Carlo generated diagnostics, using 2000 simulations incorporating between- and within-subject variability, as well as uncertainty in population parameter estimates via sampling from the posterior distribution.
NPDE vs population predictions, time and time after dose.
Population predictions are medians of 2000 simulated values incorporating between- and within-subject variability, as well as uncertainty in population parameter estimates via sampling from the posterior distribution.
NPDE vs continuous covariates
NPDE vs categorical covariates.
Expected weighted residuals (EWRES) are Monte-Carlo generated residuals, using 2000 simulations incorporating between- and within-subject variability, as well as uncertainty in population parameter estimates via sampling from the posterior distribution.
Population predictions are medians of 2000 simulated values incorporating between- and within-subject variability, as well as uncertainty in population parameter estimates via sampling from the posterior distribution.
ETAs are medians of 2000 posterior ETAs across the 4 chains.
These plots uses the yspec to automatically rename the axis labels.
Note that here we use a function that maps over the ETAs (not the covariates) because the purpose of these plots was to determine whether there were any trends in the covariates for a given ETA. This may need to be edited to address different study specific questions
## [[1]]
##
## [[2]]
##
## [[3]]
These plots uses the yspec to automatically rename the axis labels.
Note that here we use a function that maps over the covariates (not the ETAs) because the purpose of these plots was to determine whether there is any difference in the distribution of ETAs across studies, dosing groups and disease states. This should be updated to reflect the questions you’re trying to address.
## $STUDY
##
## $SEX
It is considered good practice to include these details at the end of all rmd scripts
Sys.getenv("AMI_NAME")
## [1] "metworx-22.09-20220909204110-99df9fff"
sessioninfo::session_info()
## ─ Session info ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## setting value
## version R version 4.1.3 (2022-03-10)
## os Ubuntu 18.04.6 LTS
## system x86_64, linux-gnu
## ui RStudio
## language (EN)
## collate en_US.UTF-8
## ctype en_US.UTF-8
## tz America/New_York
## date 2023-03-23
## rstudio 2022.02.1+461.pro1 Prairie Trillium (server)
## pandoc 2.17.1.1 @ /usr/lib/rstudio-server/bin/quarto/bin/ (via rmarkdown)
##
## ─ Packages ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## package * version date (UTC) lib source
## assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.1.3)
## backports 1.4.1 2021-12-13 [1] CRAN (R 4.1.3)
## bbr * 1.5.0 2023-02-04 [1] MPNDEV (R 4.1.3)
## bit 4.0.5 2022-11-15 [1] CRAN (R 4.1.3)
## bit64 4.0.5 2020-08-30 [1] CRAN (R 4.1.3)
## broom 1.0.3 2023-01-25 [1] CRAN (R 4.1.3)
## bslib 0.4.2 2022-12-16 [1] CRAN (R 4.1.3)
## cachem 1.0.6 2021-08-19 [1] CRAN (R 4.1.3)
## callr 3.7.3 2022-11-02 [1] CRAN (R 4.1.3)
## cellranger 1.1.0 2016-07-27 [1] CRAN (R 4.1.3)
## checkmate 2.1.0 2022-04-21 [1] CRAN (R 4.1.3)
## cli 3.6.0 2023-01-09 [1] CRAN (R 4.1.3)
## codetools 0.2-18 2020-11-04 [1] CRAN (R 4.1.3)
## colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.1.3)
## cowplot * 1.1.1 2020-12-30 [1] CRAN (R 4.1.3)
## crayon 1.5.2 2022-09-29 [1] CRAN (R 4.1.3)
## data.table 1.14.6 2022-11-16 [1] CRAN (R 4.1.3)
## DBI 1.1.3 2022-06-18 [1] CRAN (R 4.1.3)
## dbplyr 2.3.0 2023-01-16 [1] CRAN (R 4.1.3)
## diffobj 0.3.5 2021-10-05 [1] CRAN (R 4.1.3)
## digest 0.6.31 2022-12-11 [1] CRAN (R 4.1.3)
## dplyr * 1.0.10 2022-09-01 [1] CRAN (R 4.1.3)
## ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.1.3)
## evaluate 0.20 2023-01-17 [1] CRAN (R 4.1.3)
## fansi 1.0.4 2023-01-22 [1] CRAN (R 4.1.3)
## farver 2.1.1 2022-07-06 [1] CRAN (R 4.1.3)
## fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.1.3)
## forcats * 0.5.2 2022-08-19 [1] CRAN (R 4.1.3)
## fs 1.6.0 2023-01-23 [1] CRAN (R 4.1.3)
## furrr * 0.3.1 2022-08-15 [1] CRAN (R 4.1.3)
## future * 1.28.0 2022-09-02 [1] CRAN (R 4.1.3)
## gargle 1.2.1 2022-09-08 [1] CRAN (R 4.1.3)
## generics 0.1.3 2022-07-05 [1] CRAN (R 4.1.3)
## GGally 2.1.2 2021-06-21 [1] CRAN (R 4.1.3)
## ggplot2 * 3.4.0 2022-11-04 [1] CRAN (R 4.1.3)
## globals 0.16.2 2022-11-21 [1] CRAN (R 4.1.3)
## glue * 1.6.2 2022-02-24 [1] CRAN (R 4.1.3)
## googledrive 2.0.0 2021-07-08 [1] CRAN (R 4.1.3)
## googlesheets4 1.0.1 2022-08-13 [1] CRAN (R 4.1.3)
## gridExtra 2.3 2017-09-09 [1] CRAN (R 4.1.3)
## gridGraphics 0.5-1 2020-12-13 [1] CRAN (R 4.1.3)
## gtable 0.3.1 2022-09-01 [1] CRAN (R 4.1.3)
## haven 2.5.1 2022-08-22 [1] CRAN (R 4.1.3)
## here * 1.0.1 2020-12-13 [1] CRAN (R 4.1.3)
## highr 0.10 2022-12-22 [1] CRAN (R 4.1.3)
## hms 1.1.2 2022-08-19 [1] CRAN (R 4.1.3)
## htmltools 0.5.4 2022-12-07 [1] CRAN (R 4.1.3)
## httr 1.4.4 2022-08-17 [1] CRAN (R 4.1.3)
## inline 0.3.19 2021-05-31 [1] CRAN (R 4.1.3)
## jquerylib 0.1.4 2021-04-26 [1] CRAN (R 4.1.3)
## jsonlite 1.8.4 2022-12-06 [1] CRAN (R 4.1.3)
## knitr 1.42 2023-01-25 [1] CRAN (R 4.1.3)
## labeling 0.4.2 2020-10-20 [1] CRAN (R 4.1.3)
## latex2exp 0.9.6 2022-11-28 [1] CRAN (R 4.1.3)
## lattice 0.20-45 2021-09-22 [1] CRAN (R 4.1.3)
## lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.1.3)
## listenv 0.9.0 2022-12-16 [1] CRAN (R 4.1.3)
## loo * 2.5.1 2022-03-24 [1] CRAN (R 4.1.3)
## lubridate 1.9.1 2023-01-24 [1] CRAN (R 4.1.3)
## magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.1.3)
## Matrix 1.5-3 2022-11-11 [1] CRAN (R 4.1.3)
## matrixStats 0.63.0 2022-11-18 [1] CRAN (R 4.1.3)
## mclust 6.0.0 2022-10-31 [1] CRAN (R 4.1.3)
## mgcv 1.8-41 2022-10-21 [1] CRAN (R 4.1.3)
## modelr 0.1.10 2022-11-11 [1] CRAN (R 4.1.3)
## mrggsave * 0.4.3 2023-02-07 [1] MPNDEV (R 4.1.3)
## mrgmisc * 0.1.1 2023-02-07 [1] MPNDEV (R 4.1.3)
## mrgsolve * 1.0.9 2023-03-23 [1] MPNDEV (R 4.1.3)
## munsell 0.5.0 2018-06-12 [1] CRAN (R 4.1.3)
## nlme 3.1-161 2022-12-15 [1] CRAN (R 4.1.3)
## npde 3.3 2022-11-24 [1] CRAN (R 4.1.3)
## parallelly 1.34.0 2023-01-13 [1] CRAN (R 4.1.3)
## patchwork * 1.1.2 2022-08-19 [1] CRAN (R 4.1.3)
## pillar 1.8.1 2022-08-19 [1] CRAN (R 4.1.3)
## pkgbuild 1.4.0 2022-11-27 [1] CRAN (R 4.1.3)
## pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.1.3)
## plyr 1.8.8 2022-11-11 [1] CRAN (R 4.1.3)
## pmplots * 0.3.6 2023-02-07 [1] MPNDEV (R 4.1.3)
## pmtables * 0.5.2 2023-02-07 [1] MPNDEV (R 4.1.3)
## prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.1.3)
## processx 3.8.0 2022-10-26 [1] CRAN (R 4.1.3)
## progress 1.2.2 2019-05-16 [1] CRAN (R 4.1.3)
## ps 1.7.2 2022-10-26 [1] CRAN (R 4.1.3)
## purrr * 1.0.1 2023-01-10 [1] CRAN (R 4.1.3)
## R6 2.5.1 2021-08-19 [1] CRAN (R 4.1.3)
## RColorBrewer 1.1-3 2022-04-03 [1] CRAN (R 4.1.3)
## Rcpp 1.0.9 2022-07-08 [1] CRAN (R 4.1.3)
## RcppParallel 5.1.6 2023-01-09 [1] CRAN (R 4.1.3)
## readr * 2.1.3 2022-10-01 [1] CRAN (R 4.1.3)
## readxl 1.4.1 2022-08-17 [1] CRAN (R 4.1.3)
## renv 0.16.0 2022-09-29 [1] CRAN (R 4.1.3)
## reprex 2.0.2 2022-08-17 [1] CRAN (R 4.1.3)
## reshape 0.8.9 2022-04-12 [1] CRAN (R 4.1.3)
## RhpcBLASctl * 0.21-247.1 2021-11-05 [1] CRAN (R 4.1.3)
## rlang 1.0.6 2022-09-24 [1] CRAN (R 4.1.3)
## rmarkdown 2.20 2023-01-19 [1] CRAN (R 4.1.3)
## rprojroot 2.0.3 2022-04-02 [1] CRAN (R 4.1.3)
## rstan * 2.21.8 2023-01-17 [1] CRAN (R 4.1.3)
## rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.1.3)
## rvest 1.0.3 2022-08-19 [1] CRAN (R 4.1.3)
## sass 0.4.5 2023-01-24 [1] CRAN (R 4.1.3)
## scales 1.2.1 2022-08-20 [1] CRAN (R 4.1.3)
## sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.1.3)
## StanHeaders * 2.21.0-7 2020-12-17 [1] CRAN (R 4.1.3)
## stringi 1.7.12 2023-01-11 [1] CRAN (R 4.1.3)
## stringr * 1.5.0 2022-12-02 [1] CRAN (R 4.1.3)
## tibble * 3.1.8 2022-07-22 [1] CRAN (R 4.1.3)
## tidyr * 1.2.1 2022-09-08 [1] CRAN (R 4.1.3)
## tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.1.3)
## tidyverse * 1.3.2 2022-07-18 [1] CRAN (R 4.1.3)
## timechange 0.2.0 2023-01-11 [1] CRAN (R 4.1.3)
## tzdb 0.3.0 2022-03-28 [1] CRAN (R 4.1.3)
## utf8 1.2.2 2021-07-24 [1] CRAN (R 4.1.3)
## vctrs 0.5.2 2023-01-23 [1] CRAN (R 4.1.3)
## vroom 1.6.1 2023-01-22 [1] CRAN (R 4.1.3)
## withr 2.5.0 2022-03-03 [1] CRAN (R 4.1.3)
## xfun 0.36 2022-12-21 [1] CRAN (R 4.1.3)
## xml2 1.3.3 2021-11-30 [1] CRAN (R 4.1.3)
## xtable 1.8-4 2019-04-21 [1] CRAN (R 4.1.3)
## yaml * 2.3.7 2023-01-23 [1] CRAN (R 4.1.3)
## yspec * 0.5.3 2023-02-07 [1] MPNDEV (R 4.1.3)
##
## [1] /data/iu-nonmem-bayes-2023/renv/library/R-4.1/x86_64-pc-linux-gnu
## [2] /data/home/timw/R/renvExt
## [3] /data/iu-nonmem-bayes-2023/renv/sandbox/R-4.1/x86_64-pc-linux-gnu/dbd573bd
##
## ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
bbr::bbi_version()
## [1] "3.2.2"